package com.skirmisher.inspection.daily.service.impl;

import com.skirmisher.inspection.common.entity.InsFile;
import com.skirmisher.inspection.common.mapper.InsFileMapper;
import com.skirmisher.inspection.common.service.SystemCodeCacheService;
import com.skirmisher.inspection.common.sysenum.InsJobStatusEnum;
import com.skirmisher.inspection.daily.entity.*;
import com.skirmisher.inspection.daily.mapper.InsDailyJobMapper;
import com.skirmisher.inspection.daily.mapper.InsDaliyJobItemsMapper;
import com.skirmisher.inspection.daily.mapper.InsDaliyJobWorkerMapper;

import com.skirmisher.inspection.daily.service.InsDailyJobService;
import com.skirmisher.inspection.inspection.testController.ResponseModel;

import com.skirmisher.inspection.rectify.entity.InsJobError;
import com.skirmisher.inspection.rectify.mapper.InsJobErrotMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName InsDailyJobServiceImpl
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/13 下午2:52
 * @Version 1.0
 */
@Service
@Slf4j
@Transactional
public class InsDailyJobServiceImpl implements InsDailyJobService {

    @Autowired
    private InsDailyJobMapper insDailyJobMapper;
    @Autowired
    private InsDaliyJobItemsMapper insDaliyJobItemsMapper;
    @Autowired
    private InsDaliyJobWorkerMapper insDaliyJobWorkerMapper;
    @Autowired
    private InsJobErrotMapper insJobErrotMapper;

    @Autowired
    private SystemCodeCacheService  systemCodeCacheService;
    @Autowired
    private InsFileMapper insFileMapper;

    @Override
    public InsDailyJob getJobDetail(String jobId) {
        InsDailyJob insDailyTemplate = insDailyJobMapper.getById(jobId);
        if (insDailyTemplate == null){
            return null;
        }
        List<InsDaliyJobItems> items = insDaliyJobItemsMapper.getByTempId(jobId);
        insDailyTemplate.setItems(items);
        List<InsDaliyWorker> workers = insDaliyJobWorkerMapper.getByBindId(jobId);
        insDailyTemplate.setWorkers(workers);
        List<InsJobError> insJobErrors = insJobErrotMapper.errorList(jobId);
        for (InsJobError insJobError : insJobErrors) {
            String id = insJobError.getId();
            List<InsFile> fileList = insFileMapper.getFileList(id);
            insJobError.setFiles(fileList);
        }
        insDailyTemplate.setError(insJobErrors);
        return insDailyTemplate;
    }

    @Override
    public void getJobPageData(InsDailyJobQueryEntity query) {

        List<Object> dataList = insDailyJobMapper.pageQueryData(query);
        int count = insDailyJobMapper.pageQueryCount(query);

        query.setDateList(dataList);
        query.setCount(count);
    }

    @Override
    public void deletDaliyJob(List<String> jobIds) {

        for (String jobId : jobIds) {
            InsDailyJob job = new InsDailyJob();
            job.setId(jobId);
            job.setJobStatus("99");
            insDailyJobMapper.updateIgnoreNull(job);
        }
    }

    @Override
    public void saveDailyJob(InsDailyJobSave jobSave, ResponseModel responseModel) {

        //处理主单据数据
        InsDailyJob dailyJob = new InsDailyJob();
        String jobId = UUID.randomUUID().toString();
        if(!StringUtils.isEmpty(jobSave.getId())){
            jobId = jobSave.getId();
        }
        BeanUtils.copyProperties(jobSave, dailyJob);
        //填写其他参数
        dailyJob.setId(jobId);
        //获取模板编码值
        String nextCode = systemCodeCacheService.getNextCode(SystemCodeCacheService.DAILYJOB);
        dailyJob.setJobCode(nextCode);
        Date now = new Date();
        dailyJob.setRecTime(now);
        dailyJob.setJobStatus(InsJobStatusEnum.READY2WORK.getStatus()+"");
        dailyJob.setRecCode(jobSave.getWorkCode());
        dailyJob.setRecName(jobSave.getWorkName());

        insDailyJobMapper.insert(dailyJob);
        //处理巡查项目数据
        List<InsDaliyJobItems> items = jobSave.getItems();
        if (items!=null){
            for (InsDaliyJobItems item : items) {
                item.setItemId(UUID.randomUUID().toString());
                item.setTempId(jobId);
                if (StringUtils.isEmpty(item.getItemCode())){
                    String noDateCode = systemCodeCacheService.getNoDateCode(SystemCodeCacheService.ITEMCODE);
                    item.setItemCode(noDateCode);
                }

                insDaliyJobItemsMapper.insert(item);
            }
        }


        //处理人员数据
       List<InsDaliyJobWorker> workers = jobSave.getWorkers();
        for (InsDaliyJobWorker worker : workers) {
            worker.setId(UUID.randomUUID().toString());
            worker.setBindId(jobId);
            worker.setJobId(jobId);
            insDaliyJobWorkerMapper.insert(worker);
        }
    }

    @Override
    public void updateDaliyJob(InsDailyJobSave jobSave, ResponseModel responseModel) {

        //删除日常巡查模任务 内容 人员 周期
        String jobId = jobSave.getId();
        insDailyJobMapper.deleteJob(jobId);
        insDaliyJobItemsMapper.deleteJobItem(jobId);
        insDaliyJobWorkerMapper.deleteJobWorker(jobId);

        saveDailyJob(jobSave,responseModel);
    }

    @Override
    public void startJob(String jobId) {
        InsDailyJob job = new InsDailyJob();
        job.setId(jobId);
        job.setJobStatus(InsJobStatusEnum.WORKING.getStatus()+"");
        insDailyJobMapper.updateIgnoreNull(job);
    }

    @Override
    public void endJob(String jobId) {

        //结束任务判断下该任务是否存在整改
        InsDailyJob dailyJob = insDailyJobMapper.getById(jobId);
        String jobStatus = dailyJob.getJobStatus();
        if (!(jobStatus != null && jobStatus.equals(InsJobStatusEnum.RECTIFYING.getStatus()+""))){
            InsDailyJob job = new InsDailyJob();
            job.setId(jobId);
            job.setJobStatus(InsJobStatusEnum.COMPLIE.getStatus()+"");
            insDailyJobMapper.updateIgnoreNull(job);
        }


    }

    @Override
    public void updateConformJob2Send(InsDailyJobSave jobSave, ResponseModel responseModel) {

        //删除日常巡查模任务 内容 人员 周期
        String jobId = jobSave.getId();
        insDaliyJobWorkerMapper.deleteJobWorker(jobId);
        InsDailyJob job = new InsDailyJob();
        job.setId(jobId);
        job.setJobStatus(InsJobStatusEnum.READY2SEND.getStatus()+"");
        insDailyJobMapper.updateIgnoreNull(job);
    }

}
